/*-----------------引入资源----------------*/
import axios from 'axios'
import NProgress from 'nprogress'
import { Message } from 'element-ui'
/*------------------获得token--------------*/
let getToken = () => {
    let str = sessionStorage.getItem('user-login')
    if (!str) {
        return 'token'
    }
    return str
}
/*----------------axios的配置-----------------*/
// axios.defaults.baseURL = 'http://192.168.2.17:8010/intelligence/',
axios.defaults.baseURL = 'http://134.64.248.185:8666/intelligence/',
    axios.interceptors.request.use((req) => {
        NProgress.start()
        req.headers['Authorization'] = getToken()
        return req
    })
axios.interceptors.response.use((res) => {
    NProgress.done()
    if (res.data.code === 401 || res.data.code === 403) {
        //  Message.error('请重新登录')
        //清除token
        // window.sessionStorage.removeItem('user-login')

        // Message.error('请重新登录')

        // window.location.href =
        //     "http://134.64.116.90:9021/eam-apps/oauth/authorize?response_type=code&client_id=CTAHZSXJKYYLP20220517&redirect_uri=http://134.64.248.185:8666/clue/getToken";
    } else {
        return res
    }
    return res
})
let get = (opt) => {

    return axios.get(opt.url, {
        params: opt.data,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}
let post = (opt) => {
    // axios.responseType='blob';
    return axios.post(opt.url, opt.data)
}
let del = (opt) => {
    return axios.delete(opt.url, { params: opt.data })
}
let put = (opt) => {
    return axios.put(opt.url, opt.data)
}
let map = new Map([
    ['get', get],
    ['delete', del],
    ['post', post],
    ['put', put],
])
/*-------------------导出请求方法------------------*/
export default (opt) => {
    let mt = map.get(opt.type)
    if (!mt) {
        return new Promise((v) => {
            v({ data: null, meta: { status: 404, msg: '没有此' + opt.type + '类型的方法' } })
        })
    }
    return mt(opt)
}